package com.wn.trade.controller;

import com.wn.common.model.dto.borrow.BorrowQuery;
import com.wn.common.model.dto.borrow.BorrowRecordDTO;
import com.wn.common.model.dto.trade.bid.AgencyBidDTO;
import com.wn.common.model.vo.borrow.BorrowRecordVo;
import com.wn.common.model.vo.trade.bid.AgencyBidVo;
import com.wn.common.result.Result;
import com.wn.common.utils.Pages;
import com.wn.trade.service.IAgencyBidService;
import com.wn.trade.service.IBorrowRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 交易中心 前端控制器
 * </p>
 */
@RestController
@Api(value = "交易中心")
@NoArgsConstructor
@AllArgsConstructor
public class TradeCenterController {

    @Autowired
    private IBorrowRecordService borrowRecordService;

    @Autowired
    private IAgencyBidService agencyBidService;

    @ApiOperation("借款申请")
    @PostMapping("/auth/trade/borrow/add")
    public Result<String> addBorrowRecord(
            @RequestBody BorrowRecordDTO borrowRecordDTO) {
        String result = borrowRecordService.addBorrowRecord(borrowRecordDTO);
        return Result.ok(result);
    }

    @ApiOperation("借款查询")
    @PostMapping("/trade/borrow/search")
    public Result<Pages> searchBorrowRecord(
            @RequestBody BorrowQuery borrowQuery,
            @RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize
    ) {
        Pages<BorrowRecordVo> pages = borrowRecordService.borrowSearch(borrowQuery,currentPage,pageSize);
        return Result.ok(pages);
    }

    /**
     *  借款id
     *  approveStatus  审核状态
     *              1  同意
     *              0  审核状态
     * @param borrowRecordId
     * @param approveStatus
     * @return
     */
    @ApiOperation("管理员审核借款")
    @PostMapping("/trade/borrow/status")
    public Result<String> verifyStatus(Long borrowRecordId,String approveStatus){
        String result = borrowRecordService.verifyStatus(borrowRecordId,approveStatus);
        return Result.ok(result);
    }

    @ApiOperation("机构投标")
    @PostMapping("/auth/trade/agency/bid/add")
    public Result<AgencyBidVo> addAgencyBid(
            @RequestBody AgencyBidDTO agencyBidDTO) {
        AgencyBidVo tenderDTO=agencyBidService.addAgencyBidRecord(agencyBidDTO);
        return Result.ok(tenderDTO);
    }

    @ApiOperation("审核借款项目放款")              // 放款：接口1，管理员审核
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "borrowRecordId", value = "借款项目id"),
//            @ApiImplicitParam(name = "status", value = "借款记录的审核状态"),
//            @ApiImplicitParam(name = "commission", value = "平台佣金")
//    })
    @PutMapping("/trade/agency/bid/status") //fixme 状态参数可以删除
    public Result<String> agencyBidStatus(
            Long borrowRecordId,String status,String commission) {
        String result = agencyBidService.borrowRecordStatus(
                borrowRecordId,status,commission);
        return Result.ok(result);
    }

}